$(document).ready(() => {
    $("#loginBtn").on("click", () => {
        let username = $("#login_username").val();
        let password = $("#login_password").val();
        $('#errorText').text('')
        console.log(username, password)
        if (!username || username.length < 2) {
            $('#errorText').text('Sorry, The user name shall not be less than 2.')
            return;
        }
        if (!password || password.length < 6 || password.length > 16) {
            $('#errorText').text('Sorry, The user password length between 6-16 bit.')
            return;
        }
        const serverHost = window.config.serverHost;
        $.ajax({
            url: `${serverHost}/user/login`,
            method: "POST",
            data: {
                'username': username,
                'password': password
            },
            success: data => {
                console.log(data);
                let status = data.status;
                let result = data.data
                if (status == 'SUCCESS') {
                    window.localStorage.setItem("user", JSON.stringify(result))
                    location.href = './menu.html'
                } else {
                    $('#errorText').text("Sorry, Your Username Or Password Is Incorrect!");
                }
            },
            error: () => {
                console.log("Login Fail...");
                $('#errorText').text('SERVER ERROR!')
            }
        })
    });

    $("#registBtn").on("click", () => {
        let username = $("#regist_username").val();
        let password = $("#regist_password").val();
        let confirm = $("#regist_confirm").val();
        $('#errorText').text('')

        if (!username || username.length < 2) {
            $('#errorText').text('Sorry, The user name shall not be less than 2.')
            return;
        }
        if (!password || password.length < 6 || password.length > 16) {
            $('#errorText').text('Sorry, The user password length between 6-16 bit.')
            return;
        }
        if (password != confirm) {
            $('#errorText').text('Sorry! Your Confirm Password Must Be Same As Password!')
            return;
        }
        console.log(username, password, confirm);
        const serverHost = window.config.serverHost;
        $.ajax({
            url: `${serverHost}/user/regist`,
            method: "POST",
            data: {
                'username': username,
                'password': password
            },
            success: data => {
                console.log(data);
                let status = data.status;
                let result = data.data
                if (status == 'SUCCESS') {
                    location.href = './index.html'
                } else {
                    // let msg = data.msg
                    // console.log('MSG: ', msg)
                    $('#errorText').text("Sorry, Params Is Invalid!;");
                }
            },
            error: () => {
                console.log("Regist Fail...");
                $('#errorText').text('SERVER ERROR!')
            }
        })
    });
});